package com.txh.blog.controller.Article;

import com.alibaba.fastjson.JSONObject;
import com.txh.blog.exceprtion.exceptions.NullAttrException;
import com.txh.blog.model.Article;
import com.txh.blog.model.Label;
import com.txh.blog.service.ArticleService;
import com.txh.blog.util.ResultJsonUtils;
import com.txh.blog.vo.ArticelVo;
import com.txh.blog.vo.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @RequestMapping("/list")
    public String articleIndex(@RequestParam  Map<String,Object> param,Model model){
        Map<String,Object> pageInfo= articleService.articlePages(param,null);
        model.addAttribute("page",pageInfo);
        return "admin/blogList";
    }
    @RequestMapping("/addView")
    public String addBlogView(Model model){
        //获取到标签与类型
        Map<String,Object> result = articleService.getLablesAndTypes();
        model.addAttribute("result",result);
        return "admin/addBlog";
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultJson deleteArtilce(Long param){
        if (param == null){
            throw new NullAttrException();
        }
        articleService.deleteAtrilceById(param);
        return ResultJsonUtils.success(null);
    }

    @RequestMapping("/edit")
    public String editBlog(Long artId,Model model){
       ArticelVo articelVo = articleService.editArticleById(artId);
        //获取到标签与类型
        Map<String,Object> result = articleService.getLablesAndTypes();
        result.put("article",articelVo);
        model.addAttribute("result",result);
        return "admin/editBlog";
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultJson addBlog(Article article,String labels){
        if (labels == null){
            throw new NullAttrException();
        }
        articleService.addArticle(article,labels);
        return ResultJsonUtils.success(null);
    }

    @RequestMapping("/publish")
    @ResponseBody
    public ResultJson publish(Long param){
        if (param == null){
            throw new NullAttrException();
        }
        articleService.publishArticle(param,1);
        return ResultJsonUtils.success(null);
    }

    @ResponseBody
    @RequestMapping("/upload")
    public JSONObject uploapImg(MultipartHttpServletRequest multipartHttpServletRequest) throws IOException {
        MultipartFile file =null;
        Map<String,MultipartFile> map = multipartHttpServletRequest.getFileMap();
        for (Map.Entry<String,MultipartFile> entry:map.entrySet()){
           file =entry.getValue();
        }
        if (file == null){
            throw new NullAttrException();
        }
        JSONObject jsonObject = articleService.uploadFile(file);
        return jsonObject;
    }
}
